package main

const gitignore = `.DS_Store
.idea/
server
config/app.yaml
`
const buildsh = `svr=$(basename $(pwd))
git pull
docker-compose down
docker rmi ${svr}
docker build --no-cache -t ${svr} .
docker-compose up -d
docker image prune -f
`

const dockercompose = `version: "3.9"

services:
  app:
    container_name: "{{.}}"
    image: {{.}}
    network_mode: "innet"
    restart: always
    ports:
      - "8000:8000"
`

const dockerfile = `FROM registry.cn-hangzhou.aliyuncs.com/shortlog/go-alpine:193 as build

COPY . /project

WORKDIR /project

RUN go mod tidy && go build -o server

FROM registry.cn-hangzhou.aliyuncs.com/shortlog/alpcn:3.16

WORKDIR /app

COPY --from=build /project/server /app

EXPOSE 8000

CMD ["./server"]
`

const logsh = `docker logs -f -n 500 $(basename $(pwd))
`

const makefile = `.PHONY: proto
# build proto
proto:
	protoc --proto_path=. \
		--go_out=api \
		--go-grpc_out=api \
		api/*.proto
	go mod tidy

.PHONY: run
# run
run:
	go build -o server; ./server

.PHONY: clean
# clean run
clean:
	rm config/app.yaml server

# show help
help:
	@echo ''
	@echo 'Usage:'
	@echo ' make [target]'
	@echo ''
	@echo 'Targets:'
	@awk '/^[a-zA-Z\-\_0-9]+:/ { \
	helpMessage = match(lastLine, /^# (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")-1); \
			helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
			printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
`

const readme = `# {{.}} 项目

## 项目功能

## TODO

## 项目结构

## 数据库

## 接口说明
`

const mainTemp = `package main

import (
	"{{.}}/app"
	"{{.}}/config"
	"gitee.com/yscsky/yu"
)

func init() {
	yu.CreateFolder("config")
}

func main() {
	cfg, err := config.NewConfig("config/app.yaml")
	if err != nil {
		yu.LogErr(err, "NewConfig")
		return
	}
	yu.Run(app.NewApp(cfg))
}
`

const apiproto = `syntax = "proto3";

package {{.}};

option go_package = "/pb";

service Apis {
  rpc Add(AddReq) returns (AddResp);
}

message AddReq {
  string name = 1;
}

message AddResp {
  string name = 1;
}
`

const appgo = `package app

import (
	"context"

	"{{.}}/api/pb"
	"{{.}}/config"
	"{{.}}/service"
	"gitee.com/yscsky/yu"
	"gitee.com/yscsky/yu/yusvr"
	"github.com/labstack/echo/v4"
)

type app struct {
	cfg     *config.Config
	name    string
	svr     yusvr.IEchoServer
	service service.IService
}

func NewApp(c *config.Config) yu.IApp {
	return &app{
		cfg:     c,
		name:    c.Name,
		svr:     yusvr.NewEchoServer(c.Name, c.Http),
		service: service.NewService(c),
	}
}

func (a *app) Name() string {
	return a.name
}

func (a *app) Servers() []yu.IServer {
	return []yu.IServer{a.svr}
}

func (a *app) OnStart() bool {
	if err := a.service.Init(); err != nil {
		yu.LogErr(err, "Init")
		return false
	}
	a.svr.Health()
	g := a.svr.Group("/api", a.svr.NoCache())
	g.POST("/add", a.Add)
	return true
}

func (a *app) OnStop() {
	a.service.Close()
}

func (a *app) Add(c echo.Context) (_ error) {
	defer yu.Cost("Add")()
	req := &pb.AddReq{}
	if err := c.Bind(&req); err != nil {
		return err
	}
	resp, err := a.service.Add(context.Background(), req)
	if err != nil {
		a.svr.PbErr(c, err)
		return
	}
	a.svr.PbOK(c, resp)
	return
}
`

const configgo = `package config

import (
	"gitee.com/yscsky/yu"
	"gitee.com/yscsky/yu/dbopt"
)

// Config 项目配置
type Config struct {
	Name string
	Http string
	DSN  dbopt.DSN
}

// NewConfig 创建配置
func NewConfig(path string) (c *Config, err error) {
	c = &Config{}
	err = yu.LoadOrSaveYaml(path, c, func() interface{} {
		c = &Config{
			Name: "{{.}}",
			Http: ":8000",
			DSN: dbopt.DSN{
				Username: "postgres",
				Password: "postgres",
				URL:      "127.0.0.1",
				Port:     "5432",
				DBName:   "dbname",
				SkipTran: false,
				PreStmt:  true,
				LogLevel: 0,
			},
		}
		return c
	})
	return
}
`

const modelgo = `package ml
`

const repogo = `package repo

import (
	"gitee.com/yscsky/yu/dbopt"
)

type IRepo interface {
	Close()
	InitTables() error
	IsConflictErr(err error) bool
}

type repo struct {
	dbopt.IGormOpt
}

func NewRepo(dsn dbopt.DSN) IRepo {
	return &repo{
		IGormOpt: dbopt.MustOpenPostgres(dsn),
	}
}

func (r *repo) InitTables() error {
	// if err := r.CreateTable(&ml.xxx{}); err != nil {
	// 	yu.Logf("创建xxx表失败")
	// 	return err
	// }
	return nil
}
`

const servicego = `package service

import (
	"context"

	"{{.}}/api/pb"
	"{{.}}/config"
	"{{.}}/repo"
	"gitee.com/yscsky/yu"
)

type IService interface {
	pb.ApisServer
	Init() error
	Close()
}

type service struct {
	pb.UnimplementedApisServer
	repo repo.IRepo
}

func NewService(c *config.Config) IService {
	return &service{
		repo: repo.NewRepo(c.DSN),
	}
}

func (s *service) Init() error {
	return s.repo.InitTables()
}

func (s *service) Close() { s.repo.Close() }

func (s *service) Add(ctx context.Context, req *pb.AddReq) (resp *pb.AddResp, err error) {
	yu.Logf("name: %s", req.Name)
	resp = &pb.AddResp{Name: req.Name}
	return
}
`
